package com.ruoyi.piliangruku.controller;

import com.ruoyi.cangku.service.ICangkuService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.piliangruku.domain.PiliangRuku;
import com.ruoyi.piliangruku.domain.PiliangRuku2;
import com.ruoyi.piliangruku.service.IPiliangRukuService;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 批量入库Controller
 * 
 * @author beilian
 * @date 2023-01-10
 */
@Controller
@RequestMapping("/piliangruku/rukuTest")
public class PiliangRukuController extends BaseController
{
    private String prefix = "piliangruku/rukuTest";

    @Autowired
    private IPiliangRukuService piliangRukuService;
    @Autowired
    private ICangkuService cangkuService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @RequiresPermissions("piliangruku:rukuTest:view")
    @GetMapping()
    public String rukuTest()
    {
        return prefix + "/rukuTest";
    }

    /**
     * 查询批量入库列表
     */
    @RequiresPermissions("piliangruku:rukuTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, PiliangRuku piliangRuku)
    {
//        startPage();
//        List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
//        return getDataTable(list);
        startPage();
        String a = request.getSession().getAttribute("QuanXian").toString();

        if (a.equals("0") && (piliangRuku.getGouhuoCangku() == null || piliangRuku.getGouhuoCangku().equals(""))) {
            PiliangRuku piliangRuku1 = new PiliangRuku();
            piliangRuku1.setGouhuoCangku(request.getSession().getAttribute("name4user").toString());
            List<PiliangRuku> namee2cangku = piliangRukuService.selectPiliangRukuList(piliangRuku1);

            return getDataTable(namee2cangku);
        }else {
            startPage();
            List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
            return getDataTable(list);
        }
    }

    /**
     * 导出批量入库列表
     */
    @RequiresPermissions("piliangruku:rukuTest:export")
    @Log(title = "批量入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HttpServletRequest request,PiliangRuku piliangRuku)
    {
        String a = request.getSession().getAttribute("QuanXian").toString();
        if (a.equals("1")) {
            List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
            ExcelUtil<PiliangRuku> util = new ExcelUtil<PiliangRuku>(PiliangRuku.class);
            return util.exportExcel(list, "批量入库数据");
        }
        else {
            List<PiliangRuku2> list = piliangRukuService.selectPiliangRukuListAndQuanXian(piliangRuku);
            ExcelUtil<PiliangRuku2> util = new ExcelUtil<PiliangRuku2>(PiliangRuku2.class);
            return util.exportExcel(list, "批量入库数据");
        }
    }

    /**
     * 新增批量入库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:add")
    @Log(title = "批量入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PiliangRuku piliangRuku)
    {
        Double jiner=piliangRuku.getDijia()* piliangRuku.getShuliang();
        piliangRuku.setJiner(jiner);


        String numstr = piliangRukuService.selectGouhuoDanhaoByLastOne();
        int num;
        if (numstr == null || numstr.equals("")) {
            num = 1;
        }else {
            num = Integer.parseInt(numstr)+1;
        }

        String b = String.valueOf(num);
        if (b.length() < 8) {
            int a = 8-b.length();
            for (int j = 0; j < a; j++) {
                b="0" + b;
            }
        }
        piliangRuku.setGouhuoDanhao(b);


        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByBianHao(piliangRuku.getGoodId());
        piliangRuku.setGoodName(shangpinxinxi.getShangpinName());
        piliangRuku.setGoodGuige(shangpinxinxi.getShangpinGuige());
        piliangRuku.setShangpinLeibie(shangpinxinxi.getShangpinLeibie());

        return toAjax(piliangRukuService.insertPiliangRuku(piliangRuku));
    }

    /**
     * 修改批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PiliangRuku piliangRuku = piliangRukuService.selectPiliangRukuById(id);
        mmap.put("piliangRuku", piliangRuku);
        return prefix + "/edit";
    }

    /**
     * 修改保存批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:edit")
    @Log(title = "批量入库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PiliangRuku piliangRuku)
    {
        if (piliangRuku.getDijia() != null&&piliangRuku.getShuliang()!=null) {
            Double jiner=piliangRuku.getDijia()* piliangRuku.getShuliang();
            piliangRuku.setJiner(jiner);
        }
        return toAjax(piliangRukuService.updatePiliangRuku(piliangRuku));
    }

    /**
     * 删除批量入库
     */
    @RequiresPermissions("piliangruku:rukuTest:remove")
    @Log(title = "批量入库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(piliangRukuService.deletePiliangRukuByIds(ids));
    }


    @PostMapping( "/inserts")
    @ResponseBody
    public AjaxResult inserts(HttpServletRequest request,String msg)
    {
        PiliangRuku xiaoshouChuku = new PiliangRuku();
        Shangpinxinxi shangpinxinxi;
        if (msg.indexOf("，") >= 5||msg.indexOf(",")>=5) {
            String[] msgstr;
            if (msg.contains("，")) {
                msgstr = msg.split("，");
            }else{
                msgstr = msg.split(",");
            }

            shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByBianHao(msgstr[0]);
        }else {
            shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByBianHao(msg.replace("\n",""));
        }

        String numstr = piliangRukuService.selectGouhuoDanhaoByLastOne();
        int num;
        if (numstr == null || numstr.equals("")) {
            num = 1;
        }else {
            num = Integer.parseInt(numstr)+1;
        }

        String b = String.valueOf(num);
        if (b.length() < 8) {
            int a = 8-b.length();
            for (int j = 0; j < a; j++) {
                b="0" + b;
            }
        }
        xiaoshouChuku.setGouhuoDanhao(b);

        xiaoshouChuku.setGouhuoDate(DateUtils.getNowDate());
        xiaoshouChuku.setPaymentMethod("0");
        xiaoshouChuku.setGouhuoJingshouren("");
        xiaoshouChuku.setGouhuoCangku(request.getSession().getAttribute("name4user").toString());
        xiaoshouChuku.setGoodId(msg.replace("\n",""));
        xiaoshouChuku.setShangpinLeibie(shangpinxinxi.getShangpinLeibie());
        xiaoshouChuku.setGoodName(shangpinxinxi.getShangpinName());
        xiaoshouChuku.setGoodGuige(shangpinxinxi.getShangpinGuige());
        xiaoshouChuku.setShuliang(1.0);
        xiaoshouChuku.setDijia(shangpinxinxi.getShangpinPifajia());
        if (request.getSession().getAttribute("QuanXian").toString().equals("1")) {
            xiaoshouChuku.setZongbudijia(shangpinxinxi.getShangpinDijia());
        }

        if (shangpinxinxi.getShangpinDijia()==null){
            shangpinxinxi.setShangpinDijia(0.0);
        }
        Double jine = shangpinxinxi.getShangpinDijia()*xiaoshouChuku.getShuliang();
        xiaoshouChuku.setJiner(jine);


        if (msg.indexOf("，") >= 5||msg.indexOf(",")>=5) {
            String[] msgstr;
            if (msg.contains("，")) {
                msgstr = msg.split("，");
            }else{
                msgstr = msg.split(",");
            }
            xiaoshouChuku.setGoodId(msgstr[0]);
            xiaoshouChuku.setGoodName(msgstr[1]);
            String s = "";
            for (int i = 2; i < msgstr.length; i++) {
                s = s+msgstr[i];
            }
            xiaoshouChuku.setBeizhu(s);
        }
        return toAjax(piliangRukuService.insertPiliangRuku(xiaoshouChuku));
    }
}
